package command;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.*;

import com.oreilly.servlet.MultipartRequest;

import command.Command;
import command.CommandException;
import db.CommonConnect;
import common.CommonUtil;
import common.SaveUploadFile;
import common.UserSession;
import db.QueryManager;
import db.DAO;

/**
 * Command 인터페이스를 implement 하여 구현된 command.
 */
public class TestInsert extends Command {

	private String next;
	
	public TestInsert(String next) {
		this.next=next;
		System.out.println("next ==> "+next);
	}

	/**
	 * 실제 요청을 받아 처리하는 메소드
	 * 요청을 받으면  요청을 처리하고 next 페이지를 돌려줌
	 * 
	 * @param req HttpServletRequest
	 * @throws CommandException
	 * @return next 이동할 페이지
	 */
	public String execute(HttpServletRequest req)
	throws CommandException {
		DAO userdao = new DAO();
  
		Connection con = null;
		try {
			con = (new CommonConnect()).getConnection_ds2();
			con.setAutoCommit(false);
			
			HttpSession session = req.getSession(true);
			UserSession userSession = (UserSession)session.getAttribute("user");
			String uni_id = userSession.getUser_id();			

		
			HashMap listMap = new HashMap();
			
	// 파일 첨부 부분	
			String uploadpath=QueryManager.getQuery("upload_report.path");
			MultipartRequest multi=SaveUploadFile.getMultipartRequest(req,uploadpath);

			List list = SaveUploadFile.savefile(multi,uploadpath);
			HashMap file_info =null;
			String file_name="";
			String file_id="";
			String file_size="0";
			String originFileName = "";
			String fileInput = "";
			
			String file_name_1="";
			String file_id_1="";
			String file_size_1="0";
			String originFileName_1 = "";
			
			String file_name_2="";
			String file_id_2="";
			String file_size_2="0";
			String originFileName_2 = "";
			
			String file_name_3="";
			String file_id_3="";
			String file_size_3="0";
			String originFileName_3 = "";
			
			for ( int i=0;list!=null && list.size()>i;i++){
				file_info = (HashMap)list.get(i);
				file_name=(String)file_info.get("fileName");
				file_id=(String)file_info.get("file_id");	
				file_size= (String)file_info.get("fileSize");
				originFileName= (String)file_info.get("originFileName");
				fileInput = (String)file_info.get("fileInput");
				
				//System.out.println("originFileName====>"+originFileName);
				//System.out.println("fileInput====>"+fileInput);
				
				if (file_name != null) {  
			    	  if (fileInput.equals("attachFile_1")){
			    		  file_name_1=(String)file_info.get("fileName");
						  file_id_1=(String)file_info.get("file_id");	
						  file_size_1= (String)file_info.get("fileSize");
						  originFileName_1= (String)file_info.get("originFileName");
			    	  }
			    	  
			    	  if (fileInput.equals("attachFile_2")){
			    		  file_name_2=(String)file_info.get("fileName");
						  file_id_2=(String)file_info.get("file_id");	
						  file_size_2= (String)file_info.get("fileSize");
						  originFileName_2= (String)file_info.get("originFileName");
			    	  }
			    	  
			    	  if (fileInput.equals("attachFile_3")){
			    		  file_name_3=(String)file_info.get("fileName");
						  file_id_3=(String)file_info.get("file_id");	
						  file_size_3= (String)file_info.get("fileSize");
						  originFileName_3= (String)file_info.get("originFileName");
			    	  }
				}

			}

		
			String title = CommonUtil.nvl(multi.getParameter("s_rpt_title"));
			String cont = CommonUtil.nvl(multi.getParameter("s_rpt_cont"));
				
			

// 리퀘스트에 모두 담는다.
			req.setAttribute("msg","제출이 완료되었습니다.");
			req.setAttribute("title",title);
			req.setAttribute("cont",cont);
			
			req.setAttribute("file_name_1",file_name_1);
			req.setAttribute("file_id_1",file_id_1);
			req.setAttribute("file_size_1",file_size_1);
			req.setAttribute("originFileName_1",originFileName_1);
			
			req.setAttribute("file_name_2",file_name_2);
			req.setAttribute("file_id_2",file_id_2);
			req.setAttribute("file_size_2",file_size_2);
			req.setAttribute("originFileName_2",originFileName_2);
			
			req.setAttribute("file_name_3",file_name_3);
			req.setAttribute("file_id_3",file_id_3);
			req.setAttribute("file_size_3",file_size_3);
			req.setAttribute("originFileName_3",originFileName_3);
			

//로그를 저장한다. 필요에 따라서 사용하며 세션이 있을 경우만 쓴다.			
//			String work_text="";//무슨일을 하는지 저장하고 싶은 내용을 적을 것;
//			saveLog(con,userId,work_text);			

		}catch (Exception ex) {
			try{
				con.rollback();
			}catch(Exception ee){}
			throw new CommandException(ex.getMessage());
		}finally {
			try{
				if (con!=null) con.setAutoCommit(true);
				if (con!=null) con.close();
			}catch(Exception ee){}
		}
		return next;
	}
	


}
